//
//  SiteConfirmViewController.m
//  EasyToo
//
//  Created by Jr Lan on 13-9-2.
//
//

#import "SiteConfirmViewController.h"
#import "DeviceUtil.h"
#import "NetworkAlertView.h"
#import <QuartzCore/QuartzCore.h>

@interface SiteConfirmViewController ()

@end

@implementation SiteConfirmViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    //加载讽图
    [self init_view];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//加载视图
- (void)init_view
{
    self.title = @"景点票";
    //加载背影
    [self.view setBackgroundColor:BackgroundColor];
    
    int width = [DeviceUtil getScreenWidth];
    int heigth = self.view.frame.size.height;
    
    UIScrollView *myscrollview = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, heigth)]autorelease];
    myscrollview.directionalLockEnabled = YES; //只能一个方向滑动
    myscrollview.pagingEnabled = NO; //是否翻页
    myscrollview.backgroundColor = [UIColor whiteColor];
    myscrollview.showsVerticalScrollIndicator =YES; //垂直方向的滚动指示
    myscrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite;//滚动指示的风格
    myscrollview.showsHorizontalScrollIndicator = NO;//水平方向的滚动指示
    myscrollview.delegate = self;
    CGSize newSize = CGSizeMake(width, 500);
    [myscrollview setContentSize:newSize];
    
    //加载图片
    UIImageView *imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 280, 330)]autorelease];
    imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    imgView.image = [UIImage imageNamed:@"list2"];
    [myscrollview addSubview:imgView];
    
    //确认信息
    UILabel *confirmlbl = [[[UILabel alloc]initWithFrame:CGRectMake(20, 10, 280, 40)]autorelease];
    confirmlbl.text = @"确认信息";
    confirmlbl.font = [UIFont systemFontOfSize:20];
    confirmlbl.textColor = [UIColor redColor];
    confirmlbl.backgroundColor = [UIColor clearColor];
    confirmlbl.adjustsFontSizeToFitWidth = YES;
    confirmlbl.textAlignment = NSTextAlignmentCenter;
    [myscrollview addSubview:confirmlbl];
    
    NSMutableArray *textarray = [[NSMutableArray alloc]init];
    [textarray addObject:[NSString stringWithFormat:@"景点：%@", self.allSiteInfo.sceneName]];
    [textarray addObject:[NSString stringWithFormat:@"门票：%@", self.siteTicketInfo.sceneTicketName]];
    [textarray addObject:[NSString stringWithFormat:@"数量：%@张", self.count]];
    [textarray addObject:[NSString stringWithFormat:@"单价：￥%@", self.siteTicketInfo.salePrice]];
    [textarray addObject:[NSString stringWithFormat:@"日期：%@", self.date]];
    [textarray addObject:[NSString stringWithFormat:@"旅客姓名：%@", self.name]];
    [textarray addObject:[NSString stringWithFormat:@"联系电话：%@", self.phone]];
    [textarray addObject:[NSString stringWithFormat:@"支付方式：%@", self.siteTicketInfo.payWay]];
    int allPrice = [self.siteTicketInfo.salePrice intValue] * [self.count intValue];
    [textarray addObject:[NSString stringWithFormat:@"                                   合计：￥%d", allPrice]];
    
    int i;
    for (i = 0; i < [textarray count]; i ++) {
        UILabel *textlbl = [[[UILabel alloc]initWithFrame:CGRectMake(50, 55+31*i, 280, 30)]autorelease];
        textlbl.text = [textarray objectAtIndex:i];
        textlbl.font = [UIFont systemFontOfSize:15];
        textlbl.backgroundColor = [UIColor clearColor];
        textlbl.adjustsFontSizeToFitWidth = YES;
        [myscrollview addSubview:textlbl];
    }
    [textarray release];
    
    UIButton *nextStep =[UIButton buttonWithType:UIButtonTypeCustom];
    nextStep.frame = CGRectMake(40, 340, 230, 40);
    [nextStep setBackgroundImage:[UIImage imageNamed:@"xyb"] forState:UIControlStateNormal];
    [nextStep setTitle:@"下一步" forState:UIControlStateNormal];
    nextStep.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    nextStep.layer.borderColor = [UIColor grayColor].CGColor;
    nextStep.layer.borderWidth = 1 ;
    nextStep.layer.cornerRadius = 10 ;
    [myscrollview addSubview:nextStep];
    [nextStep addTarget:self action:@selector(confirmMessge) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:myscrollview];

}

- (void)confirmMessge
{
    SiteManager *sitemanager = [SiteManager shareManager];
    sitemanager.delegate = self;
    
    NSDate *  senddate=[NSDate date];
    NSCalendar  * cal=[NSCalendar  currentCalendar];
    NSUInteger  unitFlags=NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit;
    NSDateComponents * conponent= [cal components:unitFlags fromDate:senddate];
    NSInteger year=[conponent year];
    NSInteger month=[conponent month];
    NSInteger day=[conponent day];
    NSString * nsDateString;
    if (month > 9) {
        nsDateString = [NSString  stringWithFormat:@"%4d-%2d-%2d",year,month,day];
    }else{
        nsDateString = [NSString  stringWithFormat:@"%4d-0%d-%2d",year,month,day];
    }
    
    NSArray *dateArray =[self.date componentsSeparatedByString:NSLocalizedString(@"/", nil)];
    NSString *dateStr = [NSString stringWithFormat:@"%@-%@-%@", [dateArray objectAtIndex:0],
                         [dateArray objectAtIndex:1], [dateArray objectAtIndex:2]];
    [[NetworkAlertView shareView] showNetView];
    int allPrice = [self.siteTicketInfo.salePrice intValue] * [self.count intValue];
    [sitemanager buySiteTicket:self.name
                   reserveDate:dateStr
                 reserveNumber:self.count
                   mobilephone:self.phone
                 sceneTicketId:self.siteTicketInfo.sceneTicketId
                    mobileType:@"999"
                       webTime:nsDateString
                      allPrice:(allPrice*100)];
}


//购买门票
- (void)confirmArtInfo:(BOOL)isSuccess
                   tn:(NSString*)tn
{
    [[NetworkAlertView shareView] delView];
    if (isSuccess) {
        [UPPayPlugin startPay:tn sysProvide:nil spId:nil mode:PAY_MODE_REALEASE viewController:self delegate:self];
    } else {
        UIAlertView* baseAlert = [[[UIAlertView alloc] initWithTitle:@"下单失败" message:nil delegate:self cancelButtonTitle:@"下单失败，请重新尝试，谢谢" otherButtonTitles: nil] autorelease];
        [baseAlert show];
    }
}

-(void)UPPayPluginResult:(NSString*)result
{
    NSString *alertString = nil;
    if ([result isEqualToString:@"success"])
    {
        alertString = @"交易成功";
    }
    else if ([result isEqualToString:@"cancel"])
    {
        alertString = @"交易取消";
    }
    else
    {
        alertString = @"交易失败";
    }
    
    UIAlertView *alert = [[UIAlertView alloc]  initWithTitle:@"支付结果" message:alertString  delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
}

@end
